/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import CRUD.ForumHome;
import CRUD.ThreadHome;
import beans.MergeThreadActionForm;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.actions.DispatchAction;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import xmlMapping.Forum;
import xmlMapping.Thread;
import ModeratorOperations.ModeratorManageThreads;
import beans.SplitActionForm;
import java.util.Vector;

/**
 *
 * @author Mostafa
 */
public class MergeThreadAction extends DispatchAction {

    /* forward name="success" path="" */

    /**
     * This is the Struts action method called on
     * http://.../actionPath?method=myAction1,
     * where "method" is the value specified in <action> element : 
     * ( <action parameter="method" .../> )
     */
    public ActionForward Merge_Threads(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
      String[] ids=  request.getParameterValues("cb");
      Thread [] ts=new Thread[ids.length];
        MergeThreadActionForm mform=new MergeThreadActionForm();
        mform.setTitle("");
        mform.setThreads(ids);
      
        request.setAttribute("forumid", request.getParameter("forumid"));
        for (int i = 0; i < ids.length; i++) {
          ts[i]=  new ThreadHome().findById(Integer.parseInt(ids[i]));
        }
        mform.setThreadobjects(ts);
        request.setAttribute("MergeThreadActionForm", mform);
        
        return mapping.findForward("mergepage");
    }

    /**
     * This is the Struts action method called on
     * http://.../actionPath?method=myAction2,
     * where "method" is the value specified in <action> element : 
     * ( <action parameter="method" .../> )
     */
 
 public ActionForward Execute_Merge(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        MergeThreadActionForm mybean=(MergeThreadActionForm)form;
       Vector<Integer> forumids=new Vector<Integer>();
        for (int i = 0; i < mybean.getThreads().length; i++) {
           forumids.add(Integer.parseInt( mybean.getThreads()[i]));
        }
      
        ModeratorManageThreads moderatoroperation=new ModeratorManageThreads();
        moderatoroperation.mergeThreads(mybean.getTitle(), forumids);
        
        
        Forum fm=  new ForumHome().findById(Integer.parseInt(request.getParameter("forumid")));
          response.sendRedirect("ViewForum.jsp?forumid="+request.getParameter("forumid")+"&forumname="+fm.getName());
        return null;
    }
    
    
    
    
    
    
 public ActionForward Add_Thread(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
       
        
        response.sendRedirect("checkLogin.do?act=addThread&forumid="+request.getParameter("forumid"));
        
        return null;
    }
 
 
 public ActionForward Delete_Threads(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
       
         String[] ids=  request.getParameterValues("cb");
      Thread [] ts=new Thread[ids.length];
        MergeThreadActionForm mform=new MergeThreadActionForm();
        mform.setTitle("");
        mform.setThreads(ids);
      
        request.setAttribute("forumid", request.getParameter("forumid"));
        for (int i = 0; i < ids.length; i++) {
          ts[i]=  new ThreadHome().findById(Integer.parseInt(ids[i]));
        }
        mform.setThreadobjects(ts);
        request.setAttribute("MergeThreadActionForm", mform);
        
        return mapping.findForward("deletepage");
        
    }
    
     
    
    
     
 public ActionForward Execute_Delete(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        MergeThreadActionForm mybean=(MergeThreadActionForm)form;
       Vector<Integer> forumids=new Vector<Integer>();
        for (int i = 0; i < mybean.getThreads().length; i++) {
           forumids.add(Integer.parseInt( mybean.getThreads()[i]));
        }
      
        ModeratorManageThreads moderatoroperation=new ModeratorManageThreads();
        moderatoroperation.deleteThreads(forumids);
        
        
        Forum fm=  new ForumHome().findById(Integer.parseInt(request.getParameter("forumid")));
          response.sendRedirect("ViewForum.jsp?forumid="+request.getParameter("forumid")+"&forumname="+fm.getName());
        return null;
    }
    
    

    
    
}
